package com.press.services;

import com.press.bean.TaxonomyBean;
import com.press.mapper.TaxonomyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2016/10/9.
 */
@Service("taxonomyService")
public class TaxonomyService {
    @Resource(name = "taxonomyMapper")
    private TaxonomyMapper taxonomyMapper;

    public TaxonomyMapper getTaxonomyMapper() {
        return taxonomyMapper;
    }

    public void setTaxonomyMapper(TaxonomyMapper taxonomyMapper) {
        this.taxonomyMapper = taxonomyMapper;
    }

    //获取所有的分类
    public List<TaxonomyBean> getAll(){
        List<TaxonomyBean> list = taxonomyMapper.getAll();
        return list;
    }
    //获取已有分类所对应的文章数
    public Map<String,String> getArticleNum(List<TaxonomyBean> list){
        Map<String,String> result = new HashMap();
        for(TaxonomyBean tb : list){
            result.put(tb.getTaxonomy_id(),taxonomyMapper.getArticleNum(tb.getTaxonomy_id()));
        }
        return result;
    }
    //添加一个新的分类
    public boolean add(TaxonomyBean taxonomyBean){
        Map<String,String> para = new HashMap();
        para.put("taxonomy_title",taxonomyBean.getTaxonomy_title());
        para.put("taxonomy_description",taxonomyBean.getTaxonomy_description());
        para.put("taxonomy_icon",taxonomyBean.getTaxonomy_icon());
        para.put("taxonomy_created",taxonomyBean.getTaxonomy_created());
        return taxonomyMapper.add(para);
    }
    //修改一个分类
    public boolean edit(TaxonomyBean taxonomyBean){
        Map<String,String> para = new HashMap();
        para.put("taxonomy_id",taxonomyBean.getTaxonomy_id());
        para.put("taxonomy_title",taxonomyBean.getTaxonomy_title());
        para.put("taxonomy_description",taxonomyBean.getTaxonomy_description());
        para.put("taxonomy_created",taxonomyBean.getTaxonomy_created());
        return taxonomyMapper.edit(para);
    }
    //删除一个分类（置空映射表中的分类id再进行删除）
    public boolean delete(String taxonomy_id){
        taxonomyMapper.setnull(taxonomy_id);       //置空
        return  taxonomyMapper.delete(taxonomy_id);      //删除
    }


}
